package com.zwface.demo.utils;

import android.app.Activity;
import android.graphics.Bitmap;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.orhanobut.dialogplus.DialogPlus;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

public class DialogHelper
{
    public static int mCurrentDialogStyle = com.qmuiteam.qmui.R.style.QMUI_Dialog;
    private static QMUIDialog noTokenDlg = null;
    private static QMUIDialog noNetDlg = null;
    private static DialogPlus dialogPlus = null;

    /**
     * token失效弹窗
     */
    public static void showInvalidTokenDialog(String msg) {
        /*if( ActivityUtils.getTopActivity() != null &&
                ActivityUtils.getTopActivity().getClass().getSimpleName().equals(LoginActivity.class.getSimpleName()) ){
            return;
        }

        if( noTokenDlg == null || (noTokenDlg!=null && !noTokenDlg.isShowing()) ){
            Activity topActivity = ActivityUtils.getTopActivity();
            if (topActivity == null || topActivity.isFinishing()) return;
                    noTokenDlg = new QMUIDialog.MessageDialogBuilder(topActivity)
                            .setTitle(android.R.string.dialog_alert_title)
                            .setCancelable(false)
                            .setMessage(msg)
                            .addAction("确定", new QMUIDialogAction.ActionListener() {
                                @Override
                                public void onClick(QMUIDialog dialog, int index) {
                                    dialog.dismiss();
                                    if (!ActivityUtils.getTopActivity().getClass().getSimpleName().equals(LoginActivity.class.getSimpleName()))
                                    {
                                        ActivityUtils.startActivity(LoginActivity.class);
                                        ActivityUtils.getTopActivity().finish();
                                    }
                                }
                            }).create(mCurrentDialogStyle);
            noTokenDlg.show();
        }*/
    }

    /**
     * 网络失败弹窗
     */
    public static void showNoNetDialog() {
        if( noNetDlg == null || (noNetDlg!=null && !noNetDlg.isShowing()) ){
            Activity topActivity = ActivityUtils.getTopActivity();
            if (topActivity == null || topActivity.isFinishing()) return;
                    noNetDlg = new QMUIDialog.MessageDialogBuilder(topActivity)
                            .setTitle("网络连接失败，请检查网络")
                            .setCancelable(false)
                            .setMessage("跳转到网络设置？")
                            .addAction("确定", new QMUIDialogAction.ActionListener() {
                                @Override
                                public void onClick(QMUIDialog dialog, int index) {
                                    dialog.dismiss();
                                    NetworkUtils.openWirelessSettings();
                                }
                            })
                .create(mCurrentDialogStyle);
            noNetDlg.show();
        }
    }

    public void hideNoNetDlg(){
        if( noNetDlg != null )
            noNetDlg.dismiss();
    }

    /**
     * 显示客服电话弹窗
     */
    public static void showCallDialog(Activity activity,String number)
    {
        /*final String mobile;
        if(StringUtils.isEmpty(number))
            mobile = activity.getResources().getString(R.string.customer_mobile);
        else
            mobile = number;
        View contentView = LayoutInflater.from(activity).inflate(R.layout.dialog_call,null);
        Holder holder = new ViewHolder(contentView);
        TextView textView = contentView.findViewById(R.id.tv_number);
        textView.setText(mobile);
        DialogPlusBuilder plusBuilder = DialogPlus.newDialog(activity)
                .setContentHolder(holder)
                .setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(DialogPlus dialog, View view) {
                        switch (view.getId()){
                            case R.id.ll_call:
                                Intent dialIntent =  new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mobile));
                                if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                                    return;
                                }
                                dialIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                activity.startActivity(dialIntent);
                                break;
                        }
                    }
                });
        plusBuilder.create().show();*/
    }

    /**
     * 显示微信客服弹窗
     */
    public static void showCallWechatDialog(Activity activity)
    {
        /*View contentView = LayoutInflater.from(activity).inflate(R.layout.dialog_wechat,null);
        Holder holder = new ViewHolder(contentView);
        ImageView img = contentView.findViewById(R.id.img);
        img.setImageResource(R.mipmap.img_wechat_service);
        DialogPlusBuilder plusBuilder = DialogPlus.newDialog(activity)
                .setContentHolder(holder)
                .setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(DialogPlus dialog, View view) {
                        switch (view.getId()){
                            case R.id.ll_save:
                                Bitmap bitmap = ImageUtils.getBitmap(R.mipmap.img_wechat_service);
                                String filePath = comm.BASE_DIR+"imgs/"+System.currentTimeMillis()+".jpg";
                                ImageUtils.save(bitmap,filePath,Bitmap.CompressFormat.JPEG);

                                Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                                Uri uri = Uri.fromFile(new File(filePath));
                                intent.setData(uri);
                                activity.sendBroadcast(intent);
                                ToastUtils.showShort("保存成功");
                                break;
                        }
                    }
                });
        plusBuilder.create().show();*/
    }

    /**
     * 分享弹窗
     * @param activity
     */
    public static void showShareDialog(Activity activity, Bitmap bitmap)
    {
        /*View contentView = LayoutInflater.from(activity).inflate(R.layout.dialog_share,null);
        Holder holder = new ViewHolder(contentView);
        DialogPlusBuilder plusBuilder = DialogPlus.newDialog(activity)
                .setContentHolder(holder)
                .setOnClickListener((dialog, view) -> {
                    if(dialogPlus!=null)
                        dialogPlus.dismiss();
                    switch (view.getId()){
                        case R.id.ll_item1:
                            ShareUtils.shareWx(bitmap,0);//分享微信好友
                            break;
                        case R.id.ll_item2:
                            ShareUtils.shareWx(bitmap,1);//分享朋友圈
                            break;
                        case R.id.ll_item3:
                            String filePath = comm.BASE_DIR+"posters/"+System.currentTimeMillis()+".jpg";
                            ImageUtils.save(bitmap,filePath,Bitmap.CompressFormat.JPEG);

                            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                            Uri uri = Uri.fromFile(new File(filePath));
                            intent.setData(uri);
                            activity.sendBroadcast(intent);
                            ToastUtils.showShort("保存成功");
                            break;
                    }
                });
        dialogPlus = plusBuilder.create();
        dialogPlus.show();*/
    }
}
